package controllers;

import business.Constants;
import entity.laydata;
import entity.layimg;
import utils.Convert;
import models.files;
import net.sf.json.JSONObject;
import play.Play;
import play.db.jpa.Blob;
import utils.ErrorInfo;
import utils.FileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

public class FileController extends BaseController {
    /**
     * 图片查看
     * @param uuid
     * @throws FileNotFoundException
     */
    public static void images(String uuid) throws FileNotFoundException{
        InputStream is;
        try {
            is = new FileInputStream(new File(Blob.getStore(), uuid.split("\\.")[0]));
        } catch (Exception e) {
            is = new FileInputStream(Play.getFile("public/images/favicon.png"));
        }
        renderBinary(is);
    }
    /**
     * 上传图片
     */
    public static void uploadImage2(File file) {
        ErrorInfo error = new ErrorInfo();
        Map<String, Object> fileInfo = FileUtil.uploadFile(file,error);
        if (error.code < 0) {
            JSONObject json = new JSONObject();
            json.put("error", error);
            renderText(json.toString());
        }
        files file2=new files();
        file2.create_date=new Date();
        file2.file_realname = fileInfo.get("file_realname").toString();
        file2.filetype = fileInfo.get("filetype").toString();
        file2.filename =fileInfo.get("filename").toString();
        file2.size = Convert.strToDouble(fileInfo.get("size").toString(),0);
        file2.save();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",Constants.domain + "/FileController/images?uuid="+file2.filename);
        jsonObject.put("code",200);
        renderJSON(jsonObject);
    }
    /**
     * 上传图片
     * @param imgFile
     */
    public static void uploadImage(File imgFile) {
        ErrorInfo error = new ErrorInfo();
        Map<String, Object> fileInfo = FileUtil.uploadFile(imgFile,error);
        if (error.code < 0) {
            JSONObject json = new JSONObject();
            json.put("error", error);
            renderText(json.toString());
        }
        files file=new files();
        file.create_date=new Date();
        file.file_realname = fileInfo.get("file_realname").toString();
        file.filetype = fileInfo.get("filetype").toString();
        file.filename =fileInfo.get("filename").toString();
        file.size = Convert.strToDouble(fileInfo.get("size").toString(),0);
        file.save();
        renderJSON(fileInfo);
    }
    /**
     * layer富文本上传图片
     */
    public static void layerUploadImg(File file,String title) {
        ErrorInfo error = new ErrorInfo();
        Map<String, Object> fileInfo = FileUtil.uploadFile(file,error);
        if (error.code < 0) {
            renderJSON(error);
        }
        files file2=new files();
        file2.create_date=new Date();
        file2.file_realname = fileInfo.get("file_realname").toString();
        file2.filetype = fileInfo.get("filetype").toString();
        file2.filename =fileInfo.get("filename").toString();
        file2.size = Convert.strToDouble(fileInfo.get("size").toString(),0);
        file2.save();
        layimg li = new layimg();
        //0成功，其他失败
        li.code=0;
        li.msg="upload success";
        laydata ld = new laydata();
        ld.title=title;
        ld.src= Constants.domain + "/FileController/images?uuid="+file2.filename;
        li.data = ld;
        renderJSON(li);
    }
}
